/*
 * @lc app=leetcode.cn id=122 lang=cpp
 *
 * [122] 买卖股票的最佳时机 II
 */

// @lc code=start
class Solution {
public:
    int maxProfit(vector<int>& prices) {
        if(prices.size() < 2)
            return 0;
        int min_price = prices[0];
        int max_profit = 0;

        for(int i = 1; i < prices.size(); )
        {
            while(i < prices.size() && prices[i] <= prices[i - 1]) i++;
            if(i == prices.size())
                break;
            min_price = prices[i - 1];//买入
            while(i < prices.size() && prices[i] >= prices[i - 1]) i++;
            max_profit += prices[i - 1] - min_price;//卖出
        }

        return max_profit;
    }
};
// @lc code=end

